package com.bqy.myokgo.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.bqy.myokgo.R;
import com.bqy.myokgo.base.MyApplication;
import com.bqy.myokgo.base.manager.DialogManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

/**
 * 熊猫先生
 * 2018/10/23:14:51
 */

public class MyTool {

    /**
     * Dialog
     */
    public static Dialog initDialog(Activity activity) {
        final Dialog dialog = new Dialog(activity, R.style.dialog);
        View view = View.inflate(activity, R.layout.default_loading, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.f5_loading_img);
        Glide.with(activity).load(R.drawable.deft_icon_loading).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(view);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog2, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    DialogManager.getInstance().RemoveOkGoAll();
                    DialogManager.getInstance().RemoveHttpAll();
                    return true;
                }
                return false;
            }
        });
        return dialog;
    }

    /**
     * 获取app包名
     */
    private String getAppProcessName(Context context) {
        //当前应用pid
        int pid = android.os.Process.myPid();
        //任务管理类
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        //遍历所有应用
        List<ActivityManager.RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (info.pid == pid)//得到当前应用
                return info.processName;//返回包名
        }
        return "";
    }

    /**
     * 获取版本信息
     */
    public PackageInfo getPackageInfo() {
        PackageInfo pi = null;
        try {
            PackageManager pm = MyApplication.getContext().getPackageManager();
            pi = pm.getPackageInfo(MyApplication.getContext().getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pi;
    }

}
